// pages/user/info/info.js
import Toast from '@vant/weapp/toast/toast';
import Notify from '@vant/weapp/notify/notify';
var { generateUUID } = require('../../utils/uuid');
var { getFileExtension } = require('../../utils/file');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    userInfo: {},
    submitLoading: false
  },
  onChooseAvatar(event) {
    this.setData({ 'userInfo.tmpAvatar': event.detail.avatarUrl });
    const file = {
      url: event.detail.avatarUrl
    }
    const currDate = new Date().toLocaleDateString().replaceAll('/', '-');
    const userId = getApp().globalData.userInfo.userId;
    const filePath = 'user_picture/' + currDate + '/' + userId + '/';
    const fileName = generateUUID() + '.' + getFileExtension(file.url);
    file.fileName = filePath + fileName;
    getApp().uploadFilePromise(file.fileName, file.url)
      .then(data => {
        Toast('上传成功');
        this.setData({ 'userInfo.avatar': data.fileID });
      })
      .catch(e => {
        Toast('上传失败');
        console.log(e);
      });
  },
  onChangeToUpdUserName(event) {
    this.setData({ 'userInfo.name': event.detail.value })
  },
  onBlurToUpdPhone(event) {
    this.setData({ 'userInfo.phone': event.detail.value })
  },
  getPhoneNumber(event) {
    Toast.loading({
      message: '授权中...',
      forbidClick: true,
    });
    if (event.detail.code) {
      const getPhoneNumberParam = {
        code: event.detail.code
      }
      getApp().callContainer('/wx/getPhoneNumber', getPhoneNumberParam, (res) => {
        Toast.clear();
        const userPhone = res.data.data.phoneNumber;
        this.setData({ 'userInfo.phone': userPhone })
      }, () => {
        Toast.clear();
      });
    } else {
      Toast.clear();
    }
  },
  onClickToupdateUserInfo() {
    this.setData({ submitLoading: true });
    const param = {
      ...this.data.userInfo
    }
    getApp().callContainer('/api/user/update', param, (res) => {
      if (res.data.code == 0) {
        getApp().globalData.userInfo = this.data.userInfo;
        Notify({ type: 'success', message: '保存成功', top: this.data.CustomBar });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 500);
      }
      this.setData({ submitLoading: false });
    }, () => {
      this.setData({ submitLoading: false });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const userInfo = JSON.parse(JSON.stringify(getApp().globalData.userInfo));
    this.setData({ userInfo: userInfo })
    getApp().getTempFilePath(userInfo.avatar).then((res) => {
      this.setData({ 'userInfo.tmpAvatar': res })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})